package com.opennews.openplatform

import groovy.transform.CompileStatic
import org.springframework.http.HttpStatus

@CompileStatic
class UserActiveTokenController extends BaseController {
    static allowedMethods = [
        updateEnabled: "POST",
        delete: "POST"
    ]

    UserActiveTokenService userActiveTokenService

    // region: Get actions.
    def index() {
        render("UserActiveTokenController")
    }

    def queryDeviceByUsername() {
        def username = getClaims().username
        def result = userActiveTokenService.queryDeviceByUsername(username)

        sendDataResponse(result)
    }
    // endregion

    // region: Post actions.
    def updateEnabled() {
        def requestBodyData = getRequestBodyData()
        def id = requestBodyData.id.toString()
        def enabled = requestBodyData.enabled as boolean
        def username = getClaims().username
        userActiveTokenService.updateEnabled(id, username, enabled)

        sendStatusResponse(HttpStatus.NO_CONTENT)
    }

    def delete() {
        def requestBodyData = getRequestBodyData()
        def id = requestBodyData.id.toString()
        def username = getClaims().username
        userActiveTokenService.delete(id, username)

        sendStatusResponse(HttpStatus.NO_CONTENT)
    }
    // endregion
}
